package com.ruoyi.asset.service.impl;

import java.util.List;

import com.ruoyi.asset.domain.AssetInfo;
import com.ruoyi.asset.service.IAssetInfoService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AssetMaintainMapper;
import com.ruoyi.asset.domain.AssetMaintain;
import com.ruoyi.asset.service.IAssetMaintainService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产维修Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-12-24
 */
@Service
public class AssetMaintainServiceImpl implements IAssetMaintainService 
{
    @Autowired
    private AssetMaintainMapper assetMaintainMapper;
    @Autowired
    private IAssetInfoService assetInfoService;

    /**
     * 查询资产维修
     * 
     * @param id 资产维修主键
     * @return 资产维修
     */
    @Override
    public AssetMaintain selectAssetMaintainById(Long id)
    {
        return assetMaintainMapper.selectAssetMaintainById(id);
    }

    /**
     * 查询资产维修列表
     * 
     * @param assetMaintain 资产维修
     * @return 资产维修
     */
    @Override
    public List<AssetMaintain> selectAssetMaintainList(AssetMaintain assetMaintain)
    {
        return assetMaintainMapper.selectAssetMaintainList(assetMaintain);
    }

    /**
     * 新增资产维修
     * 
     * @param assetMaintain 资产维修
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAssetMaintain(AssetMaintain assetMaintain)
    {
        //更新设备状态为维修中
        AssetInfo assetInfo = assetInfoService.selectAssetInfoById(assetMaintain.getAssetId());
        if(!assetInfo.getStatus().equalsIgnoreCase(AssetInfo.STATUS_MAINTAIN)){
            assetInfo.setStatus(AssetInfo.STATUS_MAINTAIN);
            assetInfoService.updateAssetInfo(assetInfo);
        }

        assetMaintain.setResult(AssetMaintain.RESULT_DOING);
        assetMaintain.setCreateTime(DateUtils.getNowDate());
        return assetMaintainMapper.insertAssetMaintain(assetMaintain);
    }

    /**
     * 修改资产维修
     * 
     * @param assetMaintain 资产维修
     * @return 结果
     */
    @Override
    public int updateAssetMaintain(AssetMaintain assetMaintain)
    {
        assetMaintain.setUpdateTime(DateUtils.getNowDate());
        return assetMaintainMapper.updateAssetMaintain(assetMaintain);
    }

    /**
     * 批量删除资产维修
     * 
     * @param ids 需要删除的资产维修主键
     * @return 结果
     */
    @Override
    public int deleteAssetMaintainByIds(Long[] ids)
    {
        return assetMaintainMapper.deleteAssetMaintainByIds(ids);
    }

    /**
     * 删除资产维修信息
     * 
     * @param id 资产维修主键
     * @return 结果
     */
    @Override
    public int deleteAssetMaintainById(Long id)
    {
        return assetMaintainMapper.deleteAssetMaintainById(id);
    }
}
